﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using Framework.Library.Core.Config;

namespace Framework.Portal.Layouts.Admin.Components.Default
{
    public partial class Default : BaseControl
    {
        public string MENUNAME = string.Empty;

        public Default()
        {
        }

        public Default(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int menuId = Convert.ToInt32(Request.QueryString["m"]);
                PortalMenu info = new PortalMenu();
                info = ManagerFactory.PortalMenuManager.GetInfo(Configurations.PORTALID, menuId);
                if (info != null)
                {
                    var lst = JsonSerializer.DeserializeFromString<List<JMenuExt>>(info.PREIMAGE);
                    if (lst != null)
                    {
                        rptExtension.DataSource = lst;
                        rptExtension.DataBind();
                    }
                }
                //List<TeenioContent> lstContent = ManagerFactory.TeenioContentManager.GetNewestNews(10);
                List<TeenioContent> lstContent = new List<TeenioContent>();
                if (lstContent != null)
                {
                    rptNewest.DataSource = lstContent;
                    rptNewest.DataBind();
                }
                Dictionary<int, string> lstUser = Application.GetUser();
                if (lstUser != null)
                {
                    rptUser.DataSource = lstUser;
                    rptUser.DataBind();
                }
            }
        }

        protected void rptNewest_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var info = (TeenioContent)e.Item.DataItem;
                var hplContent = (HyperLink) e.Item.FindControl("hplContent");
                string url = Configurations.ROOTDIR + "Default.aspx?p=" + info.CONTENT_PORTALID + "&m=" + info.CONTENT_CATEGORYID +
                             "&t=1&a=" + info.CONTENT_ID;
                hplContent.Text = ConvertUtility.ToSubString(info.CONTENT_HEADLINE, 50);
                hplContent.NavigateUrl = url;
            }
        }
    }
}